#include "datapro.h"
#include "QString"
#include"QJsonArray"
#include "QJsonDocument"
#include "getres.h"
DataPro::DataPro()
{

}
UserInfoBean DataPro::Json2UserInfoBean(QJsonObject qjo)
{
    UserInfoBean uib;
    if(qjo.contains("UserID"))
        uib.UserID=qjo["UserID"].toString();
    if(qjo.contains("UserName"))
        uib.UserName=qjo["UserName"].toString();
    if(qjo.contains("UserSex"))
        uib.UserSex=qjo["UserSex"].toString();
    if(qjo.contains("UserAge"))
        uib.UserAge=qjo["UserAge"].toString();
    if(qjo.contains("UserPwd"))
        uib.UserPwd=qjo["UserPwd"].toString();
    return  uib;
}

RealTimeIndex DataPro::Json2RealTimeIndex(QJsonObject qjo)
{
    RealTimeIndex rti;
    if(qjo.contains("pr"))
        rti.pr=qjo["pr"].toInt();
    if(qjo.contains("ox"))
        rti.ox=qjo["ox"].toDouble();
    if(qjo.contains("pd"))
        rti.pd=qjo["pd"].toDouble();
    if(qjo.contains("perfusion"))
        rti.perfusion=qjo["perfusion"].toDouble();
    if(qjo.contains("eleoff"))
        rti.eleoff=qjo["eleoff"].toInt();
    return  rti;
}
PsychologyIndex DataPro::Json2PsychologyIndex(QJsonObject qjo)
{

    PsychologyIndex psy;
    if(qjo.contains("sdp"))
        psy.sdp=qjo["sdp"].toDouble();
    if(qjo.contains("rrvalid"))
    {
        psy.rrvalid=qjo["rrvalid"].toInt();
    }
    if(qjo.contains("PhysicalMentalHealth"))
        psy.PhysicalMentalHealth=qjo["PhysicalMentalHealth"].toDouble();

    if(qjo.contains("hrvIndex")&& qjo.value("hrvIndex").isArray())
    {
        QJsonArray jsonArray_hrvIndex=qjo.value("hrvIndex").toArray();
        psy.RR_mean=jsonArray_hrvIndex.at(0).toDouble();// RR间期均值
        psy.RR_CV=jsonArray_hrvIndex.at(1).toDouble(); 	//变异指数
        psy.RR_SDNN=jsonArray_hrvIndex.at(2).toDouble();	 //RR间期标准差
        psy.RR_DRR=jsonArray_hrvIndex.at(3).toDouble(); 	//RR间期极差
        psy.RR_RMSSD=jsonArray_hrvIndex.at(4).toDouble();  	//RR间期均方根值
        psy.SDSD=jsonArray_hrvIndex.at(5).toDouble(); 		//RR间期差的标准差
        psy.NN50=jsonArray_hrvIndex.at(6).toDouble(); 		//连续RR间期差大于50ms的RR间期个数
        psy.pNN50=jsonArray_hrvIndex.at(7).toDouble();  	//连续RR间期差大于50ms的RR间期个数占总数的百分比
        psy.TP=jsonArray_hrvIndex.at(8).toDouble(); 		//总功率（总能量）
        psy.vlf=jsonArray_hrvIndex.at(9).toDouble(); 	//极低频能量
        psy.lf=jsonArray_hrvIndex.at(10).toDouble(); 	//低频能量
        psy.hf=jsonArray_hrvIndex.at(11).toDouble(); 	//高低频能量
        psy.LFHF=jsonArray_hrvIndex.at(12).toDouble(); 	//低高频能量比
        psy.LFnorm=jsonArray_hrvIndex.at(13).toDouble(); 	//归一化低频能量
        psy.HFnorm=jsonArray_hrvIndex.at(14).toDouble(); 	//归一化高频能量
        psy.VAI=jsonArray_hrvIndex.at(15).toDouble(); 	//向量角度指数
        psy.VLI=jsonArray_hrvIndex.at(16).toDouble(); 	//向量长度指数
        psy.SD1=jsonArray_hrvIndex.at(17).toDouble(); 	//短轴
        psy.SD2=jsonArray_hrvIndex.at(18).toDouble(); 	//长轴
        psy.ANSState=jsonArray_hrvIndex.at(19).toDouble(); 	//自主神经功能状态(1-迷走神经兴奋，2-均衡，4-交感神经兴奋)
        psy.SNE=jsonArray_hrvIndex.at(20).toDouble(); 	//交感神经兴奋度
        psy.VNE=jsonArray_hrvIndex.at(21).toDouble(); 		//迷走神经兴奋度
        psy.Balance=jsonArray_hrvIndex.at(22).toDouble(); 	//自主神经系统平衡性
        psy.Activity=jsonArray_hrvIndex.at(23).toDouble(); 	//自主神经系统活性
        psy.Fatigue=jsonArray_hrvIndex.at(24).toDouble(); 	//身体疲劳指数
        psy.SPress=jsonArray_hrvIndex.at(25).toDouble(); 	//精神压力指数
        psy.AntiPress=jsonArray_hrvIndex.at(26).toDouble(); 	//抗压能力指数
        psy.Motion=jsonArray_hrvIndex.at(27).toDouble(); 	//精神情绪指数
        for(int i=0;i<28;i++)
        {
            psy.hrvindex[i]=jsonArray_hrvIndex.at(i).toDouble();
        }
    }
    psy.RR.clear();
    if(qjo.contains("RR")&& qjo.value("RR").isArray()){
        QJsonArray jsonArray_RR=qjo.value("RR").toArray();
        for(int i=0;i<psy.rrvalid;i++)
        {
            psy.RR.append(jsonArray_RR.at(i).toInt());
        }
    }


    if(qjo.contains("freq")&& qjo.value("freq").isArray()){
        QJsonArray jsonArray_freq=qjo.value("freq").toArray();
        for(int i=0;i<513;i++)
        {
            psy.freq[i]=jsonArray_freq.at(i).toDouble();
        }
    }
    if(qjo.contains("psd")&& qjo.value("psd").isArray()){
        QJsonArray jsonArray_psd=qjo.value("psd").toArray();
        for(int i=0;i<513;i++)
        {
            psy.psd[i]=jsonArray_psd.at(i).toDouble();
        }
    }

    return  psy;
}
GroupDataBeans DataPro::JsonObject2GroupDataBean(DATAINFO_TABLE_DATA data)
{
    getRes gr;
    QJsonDocument doc = QJsonDocument::fromJson(data.dib.TaskResult.toUtf8());
    QJsonObject jsonObject = doc.object();
    GroupDataBeans gdb;

    if(jsonObject.contains("Group_Number")){
        double Group_Number=jsonObject.value("Group_Number").toDouble();
        gdb.Group_Number=QString::number(Group_Number);
    }
    if(jsonObject.contains("UseTime")){
        double useTime=jsonObject.value("UseTime").toDouble();
        gdb.UseTime=QString::number(useTime);
    }


    //以下是团队每个人形成的数组，最终时间的值
    gdb.UserID=JsonArray2Qlist_s(jsonObject,"jsonArray_UserID");
    gdb.UserNames=JsonArray2Qlist_s(jsonObject,"jsonArray_UserName");
    DataBean db;
    QList<DataBean>dbs;
    db.UseTime=gdb.UseTime+"分钟";
    for(int i=0;i<gdb.UserID.size();i++)
    {
        if(jsonObject.contains(gdb.UserID.at(i)))
        {
            db.UserID=gdb.UserID.at(i);
            db.MeasureTime=data.MeasureTime;
            db.UserName=gdb.UserNames.at(i);
            db.Theme=data.TaskTheme;
            db.TrainContent=data.TaskContent;
            QJsonObject mydataObj=jsonObject.value(gdb.UserID.at(i)).toObject();

            if(mydataObj.contains("hrvIndex")&& mydataObj.value("hrvIndex").isArray())
            {
                QJsonArray jsonArray_hrvIndex=mydataObj.value("hrvIndex").toArray();
                gdb.MentalPressureIndexs.append(jsonArray_hrvIndex.at(25).toDouble());
                gdb.Fatigure.append(jsonArray_hrvIndex.at(24).toDouble());
                gdb.Depress.append(100-jsonArray_hrvIndex.at(27).toDouble());
                gdb.Ansiety.append(jsonArray_hrvIndex.at(27).toDouble());
                gdb.Ballance.append(jsonArray_hrvIndex.at(22).toDouble());
                gdb.Activity.append(jsonArray_hrvIndex.at(23).toDouble());
                gdb.ReMental.append(jsonArray_hrvIndex.at(26).toDouble());
                db.hrvIndex.tm.RRMean=jsonArray_hrvIndex.at(0).toDouble();
                db.hrvIndex.tm.CV=jsonArray_hrvIndex.at(1).toDouble();
                db.hrvIndex.tm.SDNN=jsonArray_hrvIndex.at(2).toDouble();
                db.hrvIndex.tm.DRR=jsonArray_hrvIndex.at(3).toDouble();
                db.hrvIndex.tm.RMSSD=jsonArray_hrvIndex.at(4).toDouble();
                db.hrvIndex.tm.SDSD=jsonArray_hrvIndex.at(5).toDouble();
                db.hrvIndex.tm.NN50=jsonArray_hrvIndex.at(6).toDouble();
                db.hrvIndex.tm.PNN50=jsonArray_hrvIndex.at(7).toDouble();

                db.hrvIndex.fm.TP=jsonArray_hrvIndex.at(8).toDouble();
                db.hrvIndex.fm.VLF=jsonArray_hrvIndex.at(9).toDouble();
                db.hrvIndex.fm.LF=jsonArray_hrvIndex.at(10).toDouble();
                db.hrvIndex.fm.HF=jsonArray_hrvIndex.at(11).toDouble();
                db.hrvIndex.fm.LFHF=jsonArray_hrvIndex.at(12).toDouble();
                db.hrvIndex.fm.LFnorm=jsonArray_hrvIndex.at(13).toDouble();
                db.hrvIndex.fm.HFnorm=jsonArray_hrvIndex.at(14).toDouble();

                db.hrvIndex.ni.VAI=jsonArray_hrvIndex.at(15).toDouble();
                db.hrvIndex.ni.VLI=jsonArray_hrvIndex.at(16).toDouble();
                db.hrvIndex.ni.SD1=jsonArray_hrvIndex.at(17).toDouble();
                db.hrvIndex.ni.SD2=jsonArray_hrvIndex.at(18).toDouble();

                db.hrvIndex.ai.ANSState=jsonArray_hrvIndex.at(19).toDouble();
                db.hrvIndex.ai.Sympathetic=jsonArray_hrvIndex.at(20).toDouble();
                db.hrvIndex.ai.Vagus=jsonArray_hrvIndex.at(21).toDouble();
                db.hrvIndex.ai.Balance=jsonArray_hrvIndex.at(22).toDouble();
                db.hrvIndex.ai.Activity=jsonArray_hrvIndex.at(23).toDouble();

                db.hrvIndex.pi.psi=jsonArray_hrvIndex.at(24).toDouble();
                db.hrvIndex.pi.mpi=jsonArray_hrvIndex.at(25).toDouble();
                db.hrvIndex.pi.rpai=jsonArray_hrvIndex.at(26).toDouble();
                db.hrvIndex.pi.ems=jsonArray_hrvIndex.at(27).toDouble();
                db.Res_HRV=gr.getRes_HRV(db.hrvIndex.tm);
                db.Res_MentalPressure=gr.getRes_MentalPressure(db.hrvIndex.pi,db.hrvIndex.ai.ANSState);




            }
            if(mydataObj.contains("rr") && mydataObj.value("rr").isArray())
            {
                QJsonArray jsonArray_rr=mydataObj.value("rr").toArray();
                int rr_len=jsonArray_rr.size();
                for(int i=0;i<rr_len;i++)
                {
                    db.RR.append(jsonArray_rr.at(i).toInt());
                }
            }

            if(mydataObj.contains("Score"))
            {
                db.PysScore=QString::number(mydataObj.value("Score").toDouble(),'f',1);
            }
            if(mydataObj.contains("HR"))
            {
                db.HR=QString::number(mydataObj.value("HR").toDouble());
            }
            if(mydataObj.contains("Ox"))
            {
                db.Ox=QString::number(mydataObj.value("Ox").toDouble());
            }
            if(mydataObj.contains("SDP"))
            {
                db.SDP=QString::number(mydataObj.value("SDP").toDouble(),'f',1);
            }
            dbs.append(db);
        }

    }

    gdb.SDP=JsonArray2Qlist(jsonObject,"jsonArray_SDP");
    gdb.PhyScore=JsonArray2Qlist(jsonObject,"jsonArray_PMH");

    gdb.dataBeans=dbs;

    //以下是团队平均趋势-随时间变化
    gdb.PhyScore_A=JsonArray2Qlist(jsonObject,"Group_A_PMH_Trend");
    return gdb;


}
BattleDataBeans DataPro::JsonObject2BattleDataBean(DATAINFO_TABLE_DATA data)
{
    getRes gr;
    QJsonDocument doc = QJsonDocument::fromJson(data.dib.TaskResult.toUtf8());
    QJsonObject jsonObject = doc.object();
    BattleDataBeans bdb;
    if(jsonObject.contains("Scene")){
        bdb.TaskContent=jsonObject.value("Scene").toString();
    }
    if(jsonObject.contains("Train_Type")){
        bdb.TaskTheme=jsonObject.value("Train_Type").toString();
    }

    if(jsonObject.contains("Group_Number")){
        double Group_Number=jsonObject.value("Group_Number").toDouble();
        bdb.Group_Number=QString::number(Group_Number);
    }

    if(jsonObject.contains("UseTime")){
        double useTime=jsonObject.value("UseTime").toDouble();
        bdb.UseTime=QString::number(useTime);
    }

    bdb.UserID=JsonArray2Qlist_s(jsonObject,"jsonArray_UserID");
    bdb.UserNames=JsonArray2Qlist_s(jsonObject,"jsonArray_UserName");
    bdb.HR=JsonArray2Qlist(jsonObject,"jsonArray_HR");
    bdb.Ox=JsonArray2Qlist(jsonObject,"jsonArray_Ox");
    DataBean db;
    QList<DataBean>dbs;
    db.UseTime=bdb.UseTime+"分钟";
    for(int i=0;i<bdb.UserID.size();i++)
    {
        if(jsonObject.contains(bdb.UserID.at(i)))
        {
            db.UserID=bdb.UserID.at(i);
            db.MeasureTime=data.MeasureTime;
            db.UserName=bdb.UserNames.at(i);
            db.Theme=data.TaskTheme;
            db.TrainContent=data.TaskContent;
            QJsonObject mydataObj=jsonObject.value(bdb.UserID.at(i)).toObject();
            if(mydataObj.contains("hrvIndex")&& mydataObj.value("hrvIndex").isArray())
            {
                QJsonArray jsonArray_hrvIndex=mydataObj.value("hrvIndex").toArray();
                bdb.MentalPressureIndexs.append(jsonArray_hrvIndex.at(25).toDouble());
                bdb.Fatigure.append(jsonArray_hrvIndex.at(24).toDouble());
                bdb.Depress.append(100-jsonArray_hrvIndex.at(27).toDouble());
                bdb.Ansiety.append(jsonArray_hrvIndex.at(27).toDouble());
                bdb.Ballance.append(jsonArray_hrvIndex.at(22).toDouble());
                bdb.Activity.append(jsonArray_hrvIndex.at(23).toDouble());
                bdb.ReMental.append(jsonArray_hrvIndex.at(26).toDouble());
                db.hrvIndex.tm.RRMean=jsonArray_hrvIndex.at(0).toDouble();
                db.hrvIndex.tm.CV=jsonArray_hrvIndex.at(1).toDouble();
                db.hrvIndex.tm.SDNN=jsonArray_hrvIndex.at(2).toDouble();
                db.hrvIndex.tm.DRR=jsonArray_hrvIndex.at(3).toDouble();
                db.hrvIndex.tm.RMSSD=jsonArray_hrvIndex.at(4).toDouble();
                db.hrvIndex.tm.SDSD=jsonArray_hrvIndex.at(5).toDouble();
                db.hrvIndex.tm.NN50=jsonArray_hrvIndex.at(6).toDouble();
                db.hrvIndex.tm.PNN50=jsonArray_hrvIndex.at(7).toDouble();

                db.hrvIndex.fm.TP=jsonArray_hrvIndex.at(8).toDouble();
                db.hrvIndex.fm.VLF=jsonArray_hrvIndex.at(9).toDouble();
                db.hrvIndex.fm.LF=jsonArray_hrvIndex.at(10).toDouble();
                db.hrvIndex.fm.HF=jsonArray_hrvIndex.at(11).toDouble();
                db.hrvIndex.fm.LFHF=jsonArray_hrvIndex.at(12).toDouble();
                db.hrvIndex.fm.LFnorm=jsonArray_hrvIndex.at(13).toDouble();
                db.hrvIndex.fm.HFnorm=jsonArray_hrvIndex.at(14).toDouble();

                db.hrvIndex.ni.VAI=jsonArray_hrvIndex.at(15).toDouble();
                db.hrvIndex.ni.VLI=jsonArray_hrvIndex.at(16).toDouble();
                db.hrvIndex.ni.SD1=jsonArray_hrvIndex.at(17).toDouble();
                db.hrvIndex.ni.SD2=jsonArray_hrvIndex.at(18).toDouble();

                db.hrvIndex.ai.ANSState=jsonArray_hrvIndex.at(19).toDouble();
                db.hrvIndex.ai.Sympathetic=jsonArray_hrvIndex.at(20).toDouble();
                db.hrvIndex.ai.Vagus=jsonArray_hrvIndex.at(21).toDouble();
                db.hrvIndex.ai.Balance=jsonArray_hrvIndex.at(22).toDouble();
                db.hrvIndex.ai.Activity=jsonArray_hrvIndex.at(23).toDouble();

                db.hrvIndex.pi.psi=jsonArray_hrvIndex.at(24).toDouble();
                db.hrvIndex.pi.mpi=jsonArray_hrvIndex.at(25).toDouble();
                db.hrvIndex.pi.rpai=jsonArray_hrvIndex.at(26).toDouble();
                db.hrvIndex.pi.ems=jsonArray_hrvIndex.at(27).toDouble();
                db.Res_HRV=gr.getRes_HRV(db.hrvIndex.tm);
                db.Res_MentalPressure=gr.getRes_MentalPressure(db.hrvIndex.pi,db.hrvIndex.ai.ANSState);
            }
            if(mydataObj.contains("rr") && mydataObj.value("rr").isArray())
            {
                QJsonArray jsonArray_rr=mydataObj.value("rr").toArray();
                int rr_len=jsonArray_rr.size();
                for(int i=0;i<rr_len;i++)
                {
                    db.RR.append(jsonArray_rr.at(i).toInt());
                }
            }

            if(mydataObj.contains("Score"))
            {
                db.PysScore=QString::number(mydataObj.value("Score").toDouble(),'f',1);
            }
            if(mydataObj.contains("HR"))
            {
                db.HR=QString::number(mydataObj.value("HR").toDouble());
            }
            if(mydataObj.contains("Ox"))
            {
                db.Ox=QString::number(mydataObj.value("Ox").toDouble());
            }
            if(mydataObj.contains("SDP"))
            {
                db.SDP=QString::number(mydataObj.value("SDP").toDouble(),'f',1);
            }
            dbs.append(db);
        }

    }
    bdb.dataBeans=dbs;
    bdb.SDP=JsonArray2Qlist(jsonObject,"jsonArray_SDP");
    bdb.PhyScore=JsonArray2Qlist(jsonObject,"jsonArray_PMH");

    bdb.PhyScore_A=JsonArray2Qlist(jsonObject,"Group_A_PhysicalMentalHealth_Trend");
    bdb.PhyScore_B=JsonArray2Qlist(jsonObject,"Group_B_PhysicalMentalHealth_Trend");
    bdb.PhyScore_C=JsonArray2Qlist(jsonObject,"Group_C_PhysicalMentalHealth_Trend");
    bdb.PhyScore_D=JsonArray2Qlist(jsonObject,"Group_D_PhysicalMentalHealth_Trend");
    bdb.Group_A_Names=jsonObject.value("Group_A_Names").toString();
    bdb.Group_B_Names=jsonObject.value("Group_B_Names").toString();
    bdb.Group_C_Names=jsonObject.value("Group_C_Names").toString();
    bdb.Group_D_Names=jsonObject.value("Group_D_Names").toString();
    bdb.GroutEixstNum=jsonObject.value("GroutEixstNum").toInt();

    return bdb;


}

QList<double>DataPro::JsonArray2Qlist(QJsonObject jsonObject,QString key)
{
    QList<double>res;
    if(jsonObject.contains(key)&& jsonObject.value(key).isArray()){
        QJsonArray jsonArray=jsonObject.value(key).toArray();
        int len=jsonArray.size();
        for(int i=0;i<len;i++)
        {
            res.append(jsonArray.at(i).toDouble());
        }
    }
    return res;
}

QList<QString>DataPro::JsonArray2Qlist_s(QJsonObject jsonObject,QString key)
{
    QList<QString>res;
    if(jsonObject.contains(key)&& jsonObject.value(key).isArray()){
        QJsonArray jsonArray=jsonObject.value(key).toArray();
        int len=jsonArray.size();
        for(int i=0;i<len;i++)
        {
            res.append(jsonArray.at(i).toString());
        }
    }
    return res;
}
